<?php
namespace app\common\controller;
use think\Controller;
use think\Db;
class Trade extends Controller
{
    // 更新出单表的应付
    public function updatePrice($tradeId)
    {
        $trade = db('Trade')->where(['id'=>$tradeId])->find();

        $price = 0;
        $price = bcadd($trade['tax_price'],$price);  // 车船税
        $price = bcadd($trade['qx_price'],$price);  // 强险
        $price = bcadd($trade['syx_price'],$price); // 商业
        $price = bcadd($trade['qtx_price'],$price); // 其他
        $price = bcadd($trade['glf_price'],$price); // 管理
        $price = bcadd($trade['gps_price'],$price); // gps
        $price = bcadd($trade['eb_price'],$price);  // 二保
        $price = bcadd($trade['xx_price'],$price);  // 学习
        $price = bcadd($trade['gz_price'],$price);  // 盖章
        $price = bcadd($trade['fw_price'],$price);  // 法务

        $qt = explode(',', $trade['qt_price']);
        foreach ($qt as $v) {
            $price = bcadd($v,$price);  // 其他
        }
        $price = bcadd($trade['fenqi_charge_amount'],$price);  // 手续费

        // 差价
        $fenqi = bcsub($trade['fenqi_amount'],$trade['fenqi_first_pay']);
        $dianfu = $trade['dianfu_amount'];
        $paid = $trade['paid'] + $fenqi + $dianfu;
        $diff = bcsub($paid,$price);

        $trade['price'] = $price;

        $trade['diff_price'] = $diff;
        db('Trade')->update($trade);
    }

    // 更新出单表的利润
    public function updateProfit($tradeId)
    {
        $trade = db('Trade')->where(['id'=>$tradeId])->find();

        # 出单表的利润 = 总价 - 保险支出

        # 找此单的保险
        $map = [
            'trade_id' => $tradeId,
            'choose' => 1
        ];
        $insurance = db('Insurance')->where($map)->select();

        $profit = $trade['price'];
        foreach ($insurance as $v) {
            // 先减去保险的价格
            $profit = $profit - $v['price'];
            // 加上保险的返点
            $profit = $profit + $v['rebate_amount'];
            // 再加上加价金额
            // $profit = $profit + $v['raise_price'];
        }

        
        
        # 减车船税
        $profit = $profit - $trade['tax_price'];

        # 减去优惠的差价
        if($trade['diff_price_type'] == '优惠' || $trade['diff_price_type'] == '利润')
        {
            $profit += $trade['diff_price'];
        }

        $trade['profit'] = $profit;
        db('Trade')->update($trade);
    }

    public function updateStatus($tradeId,$action)
    {
        $map = [
            'trade_id' => $tradeId,
            'choose' => 1
        ];
        $insurance = db('Insurance')->where($map)->select();

        # action=pay 付款之后，检查是否所有保险已经付完款，是的话status = 30待上传保单信息，不是的话还是20待付款
        if($action == 'pay')
        {
            $isPayDone = true;
            foreach ($insurance as $v) {
                if($v['cash'] == 1 && $v['pay_time'] == 0) $isPayDone = false;
            }
            if($isPayDone)
            {
                $tMap = [
                    'id' => $tradeId
                ];
                $tData = [
                    'status' => 30
                ];
                db('Trade')->where($tMap)->update($tData);
            }
        }

        if($action == 'uploadPdf')
        {
            $isUploadDone = true;
            foreach ($insurance as $v) {
                if($v['pdf'] == '' ) $isUploadDone = false;
            }
            if($isUploadDone)
            {
                $tMap = [
                    'id' => $tradeId
                ];
                $tData = [
                    'status' => 200
                ];
                db('Trade')->where($tMap)->update($tData);
            }
        }
    }

    
}
